<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 二叉树实现结构
        function Node(value) {
            this.value = value;
            this.left = null;
            this.right = null;
        }
        var a = new Node(1)
        var b = new Node(null)
        var c = new Node(2)
        var d = new Node(3)
        a.left = b
        a.right = c
        b.left = d
        // 中序遍历-迭代法
        var inorderTraversal = function (root, res = []) {
            if (!root) return []
            const stack = []
            let cur = root
            // 栈不为空
            while (stack.length || cur) {
                // 从顶部一直遍历到最左边的最底部节点
                if (cur) {
                    stack.push(cur)
                    // 左
                    cur = cur.left
                } else {
                    // 再从底部开始往上取出值，同时判断有没有右节点，有再走if(cur逻辑)
                    cur = stack.pop()
                    res.push(cur.value)
                    cur = cur.right
                }
            }
            return res
        }
        console.log(inorderTraversal(a));



    </script>
</body>

</html>